package com.example.task;

import java.util.List;

import javax.servlet.ServletContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.entity.Askers;
import com.example.entity.Students;
import com.example.service.AskersService;
import com.example.service.MyStudentService;
import com.example.service.UserService;

/**
 * 
 * 定时任务：自动分配咨询师
 * @author Lenovo
 *
 */
@RestController
@RequestMapping("askers")
@EnableScheduling//启动定时任务
public class AutoAlloZiXunTask {
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private AskersService askersService;
	@Autowired
	private MyStudentService myStudentService;
	
	@Autowired
	private ServletContext servletContext;
	
	@Scheduled(cron = " 0 */5 * * * ?")
	public void reportCurrentByCron() {
		System.out.println("--------------------开始执行自动分配咨询师任务--------------");
		//1、判断分量开关是否开启，若果没开，直接return
		String isAuto = (String) servletContext.getAttribute("autoAllotAskerSwitch");
		
		
		if (isAuto != null && isAuto.equals("off")) {
			System.out.println("分量开关已关闭，不执行自动分配，请手动分配");
			return;
		}
		//2、查询出所有的当天已签到的咨询师（安全权重从大到小排序）
		List<Askers> zxsList = askersService.selectCheckedId();
		
		
		//3、查询出所有的待分配的学生（按创建时间从小到达）
		List<Students> xsList = userService.selectAllotZXS();
		
		//获取学生人数和咨询师人数
		int xsNum = xsList.size();
		int zxsNum = zxsList.size();
		if (xsNum == 0) {
			System.out.println("没有待分配的学生...");
			return;
		}
		if (zxsNum == 0) {
			System.out.println("没有可用的咨询师...");
			return;
		}
		//boolean flag;
		//如果学生人数>=咨询师人数
		if (xsNum >= zxsNum) {
			//计算出总共分配几轮
			int lun = xsNum/zxsNum;
			//算出剩余的学生
			int shengyu = xsNum%zxsNum;
			//循环每一轮，有限分配每一轮的学生
			int tmp = 0;
			for (int k = 0; k < lun ; k++) {
				
				for (int i = 0; i < zxsNum; i++) {
					xsList.get(tmp).setAskerid(zxsList.get(i).getAskerId());
					xsList.get(tmp).setZiXunName(zxsList.get(i).getAskerName());
					tmp ++;
				}
			}
			//再分配剩余的学生
			
			for (int i = 0; i < shengyu; i++) {
					xsList.get(tmp).setAskerid(zxsList.get(i).getAskerId());
					xsList.get(tmp).setZiXunName(zxsList.get(i).getAskerName());
					tmp ++;
				}
				updateAllotZXS(xsList);
			}
			
			//如果学生人数<咨询师人数
		if (xsNum < zxsNum) {
			//根据分量权重一次循环分配
			for (int i = 0; i < xsNum; i++) {
				xsList.get(i).setAskerid(zxsList.get(i).getAskerId());
				xsList.get(i).setZiXunName(zxsList.get(i).getAskerName());
			}
			updateAllotZXS(xsList);
		}
		System.out.println("--------------------结束执行自动分配咨询师任务--------------");
		
		
	}
	//4、把分配的咨询师更新到数据库
	public void updateAllotZXS(List<Students> studentsList) {
		for (Students students : studentsList) {
			myStudentService.update(students);
		}
	}

}
